package com.example.chat.controller;

import com.aliyun.oss.model.ObjectMetadata;
import com.example.chat.result.Result;
import com.example.chat.utils.AliOssUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Set;
import java.util.UUID;

@RequestMapping("/commons")
@RestController
@CrossOrigin
@Tag(name = "图片操纵相关接口")
@Slf4j
public class CommonController {
    @Autowired
    private AliOssUtil aliOssUtil;

    @PostMapping("/upload2")
    @Operation(summary = "OSS图片上传")
    public Result upload2(MultipartFile file){
        if(!file.getContentType().startsWith("image/"))
            return Result.fail("只支持图片上传");
        try {
            String originalFilename = file.getOriginalFilename();
            String extension = originalFilename.substring(originalFilename.indexOf("."));
            String ObjectName = UUID.randomUUID() + extension;
            String filePath = aliOssUtil.upload(file.getBytes(), ObjectName);
            return Result.ok(filePath);
        } catch (IOException e) {
            log.info("文件上传失败：{}",e);
        }
        return Result.fail("文件上传失败");
    }

    @PostMapping("/upload/file")
    @Operation(summary = "OSS文件上传")
    public Result uploadFile(MultipartFile file) {
        // 允许的文件扩展名
        Set<String> allowedExtensions = Set.of("txt", "doc", "docx", "pdf", "zip", "rar", "tar");

        // 获取文件扩展名
        String originalFilename = file.getOriginalFilename();
        String extension = originalFilename.substring(originalFilename.lastIndexOf(".") + 1).toLowerCase();

        if (!allowedExtensions.contains(extension)) {
            return Result.fail("只支持上传以下文件类型：txt, doc, docx, pdf, zip, rar, tar");
        }

        try {
            String objectName = UUID.randomUUID() + "_" + originalFilename;
            String filePath = aliOssUtil.upload(file.getBytes(), objectName);
            return Result.ok(filePath);
        } catch (IOException e) {
            log.error("文件上传失败：{}", e.getMessage(), e);
            return Result.fail("文件上传失败");
        }
    }

    @PostMapping("/upload/music")
    @Operation(summary = "音乐上传")
    public Result uploadMusic(MultipartFile file){
        try {
            String originalFilename = file.getOriginalFilename();
            String extension = originalFilename.substring(originalFilename.indexOf("."));
            String ObjectName = UUID.randomUUID() + extension;
            String filePath = aliOssUtil.upload(file.getBytes(), ObjectName);
            log.info(filePath);
            return Result.ok(filePath);
        } catch (IOException e) {
            log.info("文件上传失败：{}",e);
        }
        return Result.fail("文件上传失败");
    }

}
